package com.realeyes.plugins.custom.element
{
	import flash.external.ExternalInterface;
	
	import org.osmf.elements.ImageElement;
	import org.osmf.elements.ParallelElement;
	import org.osmf.elements.ProxyElement;
	import org.osmf.events.MediaElementEvent;
	import org.osmf.events.TimeEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.layout.ScaleMode;
	import org.osmf.media.MediaElement;
	import org.osmf.media.URLResource;
	import org.osmf.traits.MediaTraitType;
	import org.osmf.traits.TimeTrait;
	
	public class CustomSampleElement extends ProxyElement
	{
		public function CustomSampleElement( proxiedElement:MediaElement=null )
		{
			super( proxiedElement );
		}
		
		override public function set proxiedElement( value:MediaElement ):void
		{
			if( value )
			{
				//Marker 5: Create the image element
				var image:ImageElement = new ImageElement( new URLResource( "assets/osmf_stacked.png" ) );
				var layout:LayoutMetadata = new LayoutMetadata();
				layout.width = 75;
				layout.height = 66;
				layout.bottom = 0;
				layout.right = 0;
				
				image.addMetadata( LayoutMetadata.LAYOUT_NAMESPACE, layout );
				
				//Marker 6: create a parallel element so we can add the image & original element
				var parallel:ParallelElement = new ParallelElement();
				
				//Marker 7: Add the proxied element and the image as children
				parallel.addChild( value );
				parallel.addChild( image );
				
				//Marker 8: Set the proxied element
				super.proxiedElement = parallel;
				
				//Marker 9: Add trait add and remove listeners
				proxiedElement.addEventListener( MediaElementEvent.TRAIT_ADD, _onAddTrait );
				proxiedElement.addEventListener( MediaElementEvent.TRAIT_REMOVE, _onRemoveTrait );
			}
		}
		
		protected function _onAddTrait( event:MediaElementEvent ):void 
		{
			switch( event.traitType )
			{
				case MediaTraitType.LOAD:
				{
					showMessage( "Media is loadable" );
					break;
				}
				case MediaTraitType.PLAY:
				{
					showMessage( "Media is playable" );
					break;
				}
				case MediaTraitType.SEEK:
				{
					showMessage( "Media is seekable" );
					break;
				}
				case MediaTraitType.TIME:
				{
					//Marker 10: Get the time trait, so we can handle the duration changed event
					var timeTrait:TimeTrait = proxiedElement.getTrait( MediaTraitType.TIME ) as TimeTrait;
					timeTrait.addEventListener( TimeEvent.DURATION_CHANGE, _onDurationChanged );
					
					showMessage( "Media has a timeline" );
					break;
				}
			}
		}
		
		protected function _onRemoveTrait( event:MediaElementEvent ):void 
		{
			showMessage( "Removing trait: " + event.traitType );
		}

		protected function _onDurationChanged( event:TimeEvent ):void 
		{
			showMessage( "New Duration: " + event.time );
		}
		
		// ==================================================
		// Helper methods
		// ==================================================
		
		protected function showMessage( msg:String ):void 
		{
			ExternalInterface.call( "showMessage", msg );
			trace( msg );
		}
	}
}